/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetointegrado;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Emerson
 */
public class DesenhaTelaMulti extends Thread {
    JanelaJogoMulti jjm;
    Canvas canvas;
    int WIDTH;
    int HEIGHT;
    KeyboardInput keyboard;
    MovementMulti mm;
    private BufferedImage char_male;
    public static Graphics2D g2d = null;
    
        public DesenhaTelaMulti(JanelaJogoMulti jjm,Canvas canvas,int WIDTH, int HEIGHT,KeyboardInput keyboard,MovementMulti mm){
            this.jjm = jjm;
            this.canvas = canvas;
            this.WIDTH = WIDTH;
            this.HEIGHT = HEIGHT;
            this.keyboard = keyboard;
            this.mm = mm;
        }
        
        public void run(){ 
        System.out.println("THE THREAD MULTIPLAYER LIIIIIIIIIIVES!");
        canvas.createBufferStrategy(1);
        BufferStrategy buffer = canvas.getBufferStrategy();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage bi = gc.createCompatibleImage( WIDTH, HEIGHT );
        Graphics graphics = null;
        Color background = Color.BLACK;
        
        while(true){
            try{
              //  System.out.println("COMEÇOU A DESENHAR");
                keyboard.poll();
               
               final Rectangle jogador = new Rectangle(PainelMultiplayer.p.x, PainelMultiplayer.p.y, PainelMultiplayer.p.w, PainelMultiplayer.p.h);
                g2d = bi.createGraphics();
                mm.Movimenta(jogador);
                g2d.setColor( background );
                g2d.fillRect( 0, 0, WIDTH, HEIGHT );
                g2d.setColor(Color.red);
              //  g2d.drawRect(PainelMultiplayer.p.x,PainelMultiplayer.p.y , PainelMultiplayer.p.h, PainelMultiplayer.p.w);
            
            try {
                    sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(DesenhaTela.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    char_male = ImageIO.read(
                                          new File(PainelMultiplayer.p.positionChar));
                } catch (IOException ex) {
                    Logger.getLogger(DesenhaTelaMulti.class.getName()).log(Level.SEVERE, null, ex);
                }
               // g2d.drawImage(char_male,PainelMultiplayer.p.getPosX(), PainelMultiplayer.p.getPosY(), jjm);
             
                //ProjetoIntegrado.pmulti.enviaObjeto();
                for(int i = 0; i < PainelMultiplayer.arrayplayer.size(); i++){
                    g2d.drawImage(char_male,PainelMultiplayer.arrayplayer.get(i).getPosX(), PainelMultiplayer.arrayplayer.get(i).getPosY(), jjm);
                    g2d.drawString(PainelMultiplayer.arrayplayer.get(i).nome,PainelMultiplayer.arrayplayer.get(i).getPosX(), PainelMultiplayer.arrayplayer.get(i).getPosY());
                }
                
             graphics = buffer.getDrawGraphics();
             graphics.drawImage( bi, 0, 0, null );
        
        if( !buffer.contentsLost() ){
          buffer.show();
          
        }
        }finally{
            if(graphics != null) 
                graphics.dispose();
                if( g2d != null ) 
                g2d.dispose();
            }
        }
        }
}
